var table;
var layer;
layui.use(['layer','table'],function(){
    table=layui.table;
    layer=layui.layer;

    //取消
    $('#cancel').on('click',function(){
        var categoryName=parent.$("input[name=categoryName]").val();
        if(categoryName!=null && categoryName!=''){
            var layer_1=parent.layer_1;
            layer_1.close(layer_1.index);
        }else{
            parent.$("#p_id span").html("");
            parent.$("input[name=categoryName]").val("");
            parent.$("input[name=categoryId]").val("");
            var layer_1=parent.layer_1;
            layer_1.close(layer_1.index);
        }
    });

});


//商品分类
var id= parent.$("#id").val();

selProductCategory(id);

function selProductCategory(id) {
    $.ajax({
        url: "/SyOperationController/selProductCategory2",
        type: "post",
        data: {"id": id},
        dataType: "json",
        success: function (data) {
            shouZTree(data.data);
        }
    });
};

//zTree
function shouZTree(zNodes) {
    var setting = {
        check: {
            enable: true,
            chkStyle: "radio",
            radioType: "level"
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        view:{
            showIcon: false,
        },
        callback: {
            beforeCheck: beforeCheck,
            onCheck: onCheck,
            beforeClick: zTreeBeforeClick
        }
    };

    function zTreeBeforeClick(treeId, treeNode, clickFlag) {
        return !treeNode.isParent;//当是父节点 返回false 不让选取
    };

    var code;
    function setCheck() {
        var type = $("#level").attr("checked")? "level":"all";
        setting.check.radioType = type;
        showCode('setting.check.radioType = "' + type + '";');
        $.fn.zTree.init($("#treeDemo"), setting, zNodes);
    }
    function showCode(str) {
        if (!code) code = $("#code");
        code.empty();
        code.append("<li>"+str+"</li>");
    }

    $(document).ready(function(){
        setCheck();
        $("#level").bind("change", setCheck);
        $("#all").bind("change", setCheck);
    });


    var code, log, className = "dark";
    function beforeCheck(treeId, treeNode) {
        className = (className === "dark" ? "":"dark");
        showLog("[ "+getTime()+" beforeCheck ]&nbsp;&nbsp;&nbsp;&nbsp;" + treeNode.name );
        return (treeNode.doCheck !== false);
    }



    //处理默认选择的分类
    var id= parent.$("#id").val();
    onCheck1(id);
    function onCheck1(id) {
        if(id){
            $.ajax({
                url: "/SyOperationController/selChildParent",
                type: "post",
                dataType: "json",
                data: {"id": id},
                success: function (data) {
                     if(data.data.pId!=1 && data.data.pId!=null ){
                        onCheck1Parent(data.data.pId,data.data.name);
                    }else{
                        $("#p_id span").replaceWith("<span>"+data.data.name+"</span>");
                    }
                }
            });
        }
    }
    function onCheck1Parent(pId,name) {
        $.ajax({
            url: "/SyOperationController/selChildParent",
            type: "post",
            dataType: "json",
            data: {"id": pId},
            success: function (data) {
                $("#p_id span").replaceWith("<span>"+data.data.name+" > "+name+"</span>");
            }
        });
    }



    function onCheck(e, treeId, treeNode) {

        //判断是否是根节点
        if(treeNode.pId!=0 && treeNode.pId!=null){
            $.ajax({
                url: "/SyOperationController/selChildParent",
                type: "post",
                dataType: "json",
                data:{"id":treeNode.pId},
                success: function (data) {

                    if(data.data.parent){
                        $("#p_id span").replaceWith("<span>"+data.data.parent.name+" > "+data.data.name+" > "+treeNode.name+"</span>");
                    }else{
                        $("#p_id span").replaceWith("<span>"+data.data.name+" > "+treeNode.name+"</span>");
                    }
                    //确定
                    $("#sure").on('click',function(){
                        //获取根节点
                        if(data.data.parent){
                            $("#p_id span").replaceWith("<span>"+data.data.parent.name+" > "+data.data.name+" > "+treeNode.name+"</span>");

                            parent.$("#p_id span").replaceWith("<span>"+data.data.parent.name+" > "+data.data.name+" > "+treeNode.name+"</span>");
                            parent.$("input[name=categoryName]").val(treeNode.name);
                            parent.$("input[name=categoryId]").val(treeNode.id);
                        }else{
                            $("#p_id span").replaceWith("<span>"+data.data.name+" > "+treeNode.name+"</span>");

                            parent.$("#p_id span").replaceWith("<span>"+data.data.name+" > "+treeNode.name+"</span>");
                            parent.$("input[name=categoryName]").val(treeNode.name);
                            parent.$("input[name=categoryId]").val(treeNode.id);
                        }

                        var layer_1=parent.layer_1;
                        layer_1.close(layer_1.index);
                    })

                }
            });
        }else if(treeNode.pId==null){
            layer.msg('请选择有效分类', {icon: 5});
        }else{
            $("#p_id span").replaceWith("<span>"+treeNode.name+"</span>");
            //确定
            $("#sure").on('click',function(){
                $("#p_id span").replaceWith("<span>"+treeNode.name+"</span>");

                parent.$("#p_id span").replaceWith("<span>"+treeNode.name+"</span>");
                parent.$("input[name=categoryName]").val(treeNode.name);
                parent.$("input[name=categoryId]").val(treeNode.id);

                var layer_1=parent.layer_1;
                layer_1.close(layer_1.index);
            })
        }

        showLog("[ "+getTime()+" onCheck ]&nbsp;&nbsp;&nbsp;&nbsp;" + treeNode.name );
    }



    function showLog(str) {
        if (!log) log = $("#log");
        log.append("<li class='"+className+"'>"+str+"</li>");
        if(log.children("li").length > 6) {
            log.get(0).removeChild(log.children("li")[0]);
        }
    }


    function getTime() {
        var now= new Date(),
            h=now.getHours(),
            m=now.getMinutes(),
            s=now.getSeconds(),
            ms=now.getMilliseconds();
        return (h+":"+m+":"+s+ " " +ms);
    }


}